﻿namespace HuaWei.CommClass
{
    using HuaWei.BusinessRule;
    using HuaWei.DAI;
    using HuaWei.Windows.Forms;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.InteropServices;

    public static class DBHelp
    {
        public static IDataAccess IDA = null;

        public static void ExecuteDataSet(DataSet ds, string sql, string name)
        {
            IDataAccess access = IniIDA();
            ds.Merge(access.ExecuteDataSet(sql, CommandType.Text, name));
        }

        public static bool ExeSql(string sql)
        {
            return ExeSql(IniIDA(), sql);
        }

        public static bool ExeSql(IDataAccess ida, ArrayList sqlList)
        {
            try
            {
                ida.ExecuteNonQuery(sqlList, CommandType.Text);
                return true;
            }
            catch (Exception exception)
            {
                MsgBox.ShowQuestion(exception.ToString());
                return false;
            }
        }

        public static bool ExeSql(IDataAccess ida, string sql)
        {
            try
            {
                ida.ExecuteNonQuery(sql, CommandType.Text);
                return true;
            }
            catch (Exception exception)
            {
                MsgBox.ShowQuestion(exception.ToString());
                return false;
            }
        }

        public static DataTable GetDt(string sql, string tableName)
        {
            return GetDt(IniIDA(), sql, tableName);
        }

        public static DataTable GetDt(IDataAccess ida, string sql, string tableName)
        {
            return ida.ExecuteDataSet(sql, (CommandType)1, tableName).Tables[tableName];
        }

        public static IDataAccess IniIDA()
        {
            return IDA;
        }

        public static IDataAccess IniIDA(string dbType, string dbName)
        {
            string username = "";
            string password = "DB*((899";
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            CBaseRule rule = new CBaseRule();
            return rule.GetDataAccess(dbType, baseDirectory, dbName, username, password);
        }

        public static void save(DataSet ds)
        {
            IDataAccess access = IniIDA();
            access.OpenDatabase();
            access.BeginTrans();
            try
            {
                access.Update(ds, true);
                access.Commit();
                MsgBox.ShowQuestion("数据已成功保存！");
            }
            catch (Exception exception)
            {
                access.RollBack();
                MsgBox.ShowError("数据保存时出错，请与管理员系统！", exception);
            }
            finally
            {
                access.CloseDatabase();
            }
        }

        public static DataTable SetDs(DataSet ds, string sql, string tableName, IDataAccess ida)
        {
            DataTable table = ida.ExecuteDataSet(sql, (CommandType)1, tableName).Tables[tableName];
            ds.Merge(table);
            return ds.Tables[tableName];
        }

        public static DataTable SetDt(DataSet ds, string tableName, string condition, IDataAccess ida)
        {
            try
            {
                string exeStr = string.Format("select * from {0} where {1}", tableName, condition);
                DataTable table = ida.ExecuteDataSet(exeStr, (CommandType)1, tableName).Tables[tableName];
                ds.Merge(table);
                return ds.Tables[tableName];
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool Unique(string tableName, string fieldName, string value, [Optional, DefaultParameterValue("")] string condition)
        {
            IDataAccess access = IniIDA();
            string exeStr = string.Format("select {0} from {1} where {0}='{2}'", fieldName, tableName, value);
            if (!condition.Equals(""))
            {
                exeStr = exeStr + " and " + condition;
            }
            object obj2 = access.ExecuteScalar(exeStr, CommandType.Text);
            return ((obj2 != null) && !obj2.Equals(""));
        }
    }
}

